<?php

namespace Admin\Service;
use Zend\Form\FormInterface as Form;

use User\Service\User as UserService;
use Admin\Mapper\AdminUser as AdminUserMapper;
//use RoleUser\Service\RoleAdminUser as RoleAdminUser;

class AdminUser extends UserService
{
    protected $createUserForm;

    public function createUser(array $data)
    {
        $entityClass = $this->getOptions()->getUserEntityClass();
        $form = $this->getCreateUserForm();
        $form->setHydrator($this->getFormHydrator());
        $form->bind(new $entityClass);
            
        $form->setData($data);

        if($form->isValid())
        {
            $user = $form->getData(); //var_dump($user);

            $events= $this->getEventManager();
            $user->setPassword($this->getFormHydrator()->getCryptoService()->create($user->getPassword()));
            $user->setCreatedOn(strtotime('now'));
            //$events->trigger(__FUNCTION__, $this, compact('user', 'form'));
            $this->getUserMapper()->insert($user);
            $events->trigger(__FUNCTION__, $this, compact('user', 'form'));
            //$events->trigger(__FUNCTION__.'.post', $this, compact('user', 'form'));

            return $user;
        }
        else
        {
            //var_dump($data);
        }
        //die(print 'invalid');
        return false;
    }

    public function updateUser(array $data)
    {
        $entityClass = $this->getOptions()->getUserEntityClass();
        $form = $this->getCreateUserForm();
        //$form->setHydrator($this->getFormHydrator());
        //$form->bind($entityUser);

        if($form->isValid())
        {
            $user = $form->getData();

            $events= $this->getEventManager();

            $user->setPassword($this->getFormHydrator()->getCryptoService()->create($user->getPassword()));
            $user->setCreatedOn(strtotime('now'));
            
            $user->usetKey('roles');
            $user->usetKey('role_id');
        
            $events->trigger(__FUNCTION__, $this, compact('user', 'form'));
            $this->getUserMapper()->update($user);
            $events->trigger(__FUNCTION__.'.post', $this, compact('user', 'form'));

            /*--------------------------------------------------------------------*/
            /*$role = new \Authorize\Acl\Role(2 , null);
            $user->addRole($role);
            $roleAdminUser = new RoleAdminUser();
            $roleAdminUser->createRoleUser($user);*/
            $userRole = array(
                'user_id' => $user->getUserId(),
                'role_id' => $data['role_id'] ,
            );
            $this->serviceManager->get('user_role_mapper')->update_user_role_link($userRole);
            /*--------------------------------------------------------------------*/
            return $user;
        }
        return false;
    }

    public function deteleUser($user_id)
    {
        if((int)$user_id > 0)
        {
            $this->getUserMapper()->delete($where = array('user_id' => $user_id));
        }

        $this->serviceManager->get('user_role_mapper')->delete_user_role_link($where = array('user_id' => $user_id));
    }

    /*
    * get userMapper
    * return userMapper
    */
    public function getUserMapper()
    {
        if(!$this->userMapper)
        {
            $this->setUserMapper($this->serviceManager->get('admin_user_mapper'));
        }
        return $this->userMapper;
    }

    /*
     * set userMapper
     *
     */
    public function setUserMapper(AdminUserMapper $userMapper)
    {
        $this->userMapper = $userMapper;
        return $this;
    }



    public function getCreateUserForm()
    {
        if (!$this->createUserForm) {
            $this->setCreateUserForm($this->serviceManager->get('admin_create_form'));
        }
        return $this->createUserForm;
    }

    public function setCreateUserForm(Form $createUserForm)
    {
        $this->createUserForm = $createUserForm;
    }
}